import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_shop_z/pages/home_page.dart';
import 'package:flutter_shop_z/pages/category_page.dart';
import 'package:flutter_shop_z/pages/cart_page.dart';
import 'package:flutter_shop_z/pages/index_page/bloc/index_count_bloc.dart';
import 'package:flutter_shop_z/pages/member_page.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_shop_z/provides/currentIndex.dart';

class IndexPage extends StatefulWidget {
  IndexPage();
  _IndexPage createState() {
    return _IndexPage();
  }
}

class _IndexPage extends State<IndexPage> {
  PageController _pageController; //保持页面
  int currentIndex = 0;
  var currentPage;
  _IndexPage();
  final List<BottomNavigationBarItem> bottomBars = [
    BottomNavigationBarItem(icon: Icon(CupertinoIcons.home), label: "首页"),
    BottomNavigationBarItem(icon: Icon(CupertinoIcons.search), label: "分类"),
    BottomNavigationBarItem(
        icon: Icon(CupertinoIcons.shopping_cart), label: "购物车"),
    BottomNavigationBarItem(
        icon: Icon(CupertinoIcons.profile_circled), label: "会员中心"),
  ];

  final List<Widget> pageViewLists = [
    HomePage(),
    CategoryPage(0),
    CartPage(),
    MemberPage()
  ];

  @override
  void initState() {
    currentPage = pageViewLists[currentIndex];
    _pageController = new PageController()
      ..addListener(() {
        if (currentPage != _pageController.page.round() || currentIndex == 1) {
          setState(() {
            currentIndex = _pageController.page.round();
          });
        }
      });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(
        BoxConstraints(
            maxWidth: MediaQuery.of(context).size.width,
            maxHeight: MediaQuery.of(context).size.height),
        designSize: Size(750, 1334),
        orientation: Orientation.portrait);
    return BlocBuilder<IndexCountBloc, IndexCountState>(
        builder: (context, state) {
      print("state.current ===>${state.current}");
      if (state.current != currentIndex) {
        currentIndex = state.current;
        currentPage = pageViewLists[currentIndex];
      }
      return Scaffold(
        backgroundColor: Color.fromRGBO(244, 245, 245, 1.0),
        bottomNavigationBar: BottomNavigationBar(
          items: bottomBars,
          currentIndex: currentIndex,
          fixedColor: Colors.purple,
          unselectedItemColor: Colors.purple,
          selectedFontSize: 15.0,
          type: BottomNavigationBarType.shifting,
          onTap: (index) {
            print("state.current != index===>${state.current} != ${index}");
            setState(() {
              BlocProvider.of<IndexCountBloc>(context)
                  .add(SetIndexCountEvent(index));
              currentIndex = index;
              currentPage = pageViewLists[currentIndex];
            });
          },
        ),
        body: IndexedStack(
          index: state.current,
          children: pageViewLists,
        ),
      );
    });
  }
}
